package com.ruoyi.ax.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ax.domain.AxAuthorizationSell;
import com.ruoyi.ax.domain.AxIndexSaleInfo;
import com.ruoyi.ax.service.IAxGoodService;
import com.ruoyi.ax.service.IAxRoundService;
import com.ruoyi.ax.service.IAxUserService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.OrderStatus;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.sun.net.httpserver.Authenticator;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ax.domain.AxOrder;
import com.ruoyi.ax.service.IAxOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author ruoyi
 * @date 2025-05-07
 */
@RestController
@RequestMapping("/ax/axOrder")
public class AxOrderController extends BaseController
{
    @Autowired
    private IAxOrderService axOrderService;




    /**
     * 查询订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AxOrder axOrder)
    {
        startPage();
        List<AxOrder> list = axOrderService.selectAxOrderList(axOrder);
        return getDataTable(list);
    }


    /**
     * 购买商品
     */
    @Log(title = "购买商品", businessType = BusinessType.OTHER)
    @PostMapping("/buyGood")
    public synchronized AjaxResult  buyGood(@RequestBody    AxOrder axOrder)
    {

        return  toAjax(axOrderService.buyGood(axOrder));
    }


    /**
     * 购买B平台普通商品
     */
    @Log(title = "购买B平台普通商品", businessType = BusinessType.OTHER)
    @PostMapping("/buyPbGood")
    public synchronized AjaxResult  buyPbGood(@RequestBody   AxOrder axOrder)
    {
        return  toAjax(axOrderService.buyPbGood(axOrder));
    }

    /**
     * 购买B平台津贴商品
     */
    @Log(title = "购买B平台津贴商品", businessType = BusinessType.OTHER)
    @PostMapping("/buyAllowanceGood")
    public synchronized AjaxResult  buyAllowanceGood( @RequestBody   AxOrder axOrder)
    {
        return  toAjax(axOrderService.buyAllowanceGood(axOrder));
    }



    @Log(title = "委拍商品", businessType = BusinessType.OTHER)
    @PostMapping("/sellGood")
    public synchronized  AjaxResult sellGood(@RequestBody   AxAuthorizationSell axAuthorizationSell)
    {
        return  toAjax(axOrderService.sellGood(axAuthorizationSell));
    }



    @GetMapping("/myshoplist")
    public TableDataInfo myshoplist(AxOrder axOrder)
    {
        startPage();
        if(StringUtils.isEmpty(axOrder.getPlatform())){
            axOrder.setPlatform("A");
        }
        axOrder.setSysUserId(SecurityUtils.getUserId());
        List<AxOrder> list = axOrderService.selectMyAxOrderList(axOrder);
        return getDataTable(list);
    }



    /**
     * 导出订单列表
     */
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AxOrder axOrder)
    {
        List<AxOrder> list = axOrderService.selectAxOrderList(axOrder);
        for(AxOrder axOrder1:list){
            SysUser sysUser = axOrder1.getSysUser();
            BeanUtils.copyProperties(sysUser,axOrder1);
            axOrder1.setRoundName(axOrder1.getAxGood().getAxRound().getRoundName());
            axOrder1.setGoodName(axOrder1.getAxGood().getGoodName());
          //  axOrder1.setGoodImg(axOrder1.getAxGood().getGoodImg());
        }
        ExcelUtil<AxOrder> util = new ExcelUtil<AxOrder>(AxOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(axOrderService.selectAxOrderById(id));
    }

    /**
     * 新增订单
     */
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AxOrder axOrder)
    {
        return toAjax(axOrderService.insertAxOrder(axOrder));
    }

    /**
     * 修改订单
     */
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AxOrder axOrder)
    {
        return toAjax(axOrderService.updateAxOrder(axOrder));
    }


    /**
     * 收货
     */
    @Log(title = "收货", businessType = BusinessType.UPDATE)
    @PostMapping("/receiveGood")
    public AjaxResult receiveGood(@RequestBody AxOrder axOrder)
    {
        axOrder.setReceiveTime(new Date());
        axOrder.setOrderState(Long.valueOf(OrderStatus.已完成.getCode()));
        return toAjax(axOrderService.updateAxOrder(axOrder));
    }

    /**
     * 发货
     */
    @Log(title = "发货", businessType = BusinessType.UPDATE)
    @PostMapping("/sendGood")
    public AjaxResult sendGood(@RequestBody AxOrder axOrder)
    {
        axOrder.setSendTime(new Date());
        axOrder.setOrderState(Long.valueOf(OrderStatus.待收货.getCode()));
        return toAjax(axOrderService.updateAxOrder(axOrder));
    }

    /**
     * 删除订单
     */
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(axOrderService.deleteAxOrderByIds(ids));
    }

    /**
     * 首页统计数据
     */
    @GetMapping(value = "/indexStaticSummary")
    public AjaxResult indexStaticSummary()
    {
        AxIndexSaleInfo axIndexSaleInfo=  axOrderService.selectAxIndexSaleInfo(new AxOrder());
        return success(axIndexSaleInfo);
    }


    @GetMapping(value = "/indexStaticWeekDayData")
    public AjaxResult indexStaticWeekDayData()
    {
        List<AxIndexSaleInfo> list=  axOrderService.selectAxIndexSaleInfoByWeek(new AxOrder());
        return success(list);
    }

}
